package com.byxm.wlgw.service;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import com.byxm.wlgw.bean.DdspxxBean;
import com.byxm.wlgw.bean.DdxxBean;
import com.byxm.wlgw.common.DataBaseAccess;
import com.byxm.wlgw.common.Pages;
import com.byxm.wlgw.query.DdxxQuery;

public class DdxxService {

	
	
	DdxxQuery ddxxQuery=new DdxxQuery();
	DataBaseAccess dba=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	public List<DdxxBean> findListDdxx(DdxxBean ddxxBean,Pages page) throws Exception{
		List<DdxxBean> lstDdxx=new ArrayList<DdxxBean>();
		try {
			dba=new DataBaseAccess();
			rs=dba.executeQuery(ddxxQuery.findDdxxAll(ddxxBean, page));
			while(rs.next()){
				DdxxBean ddxxBean2=new DdxxBean();
				ddxxBean2.setDdxxid(rs.getString("ddxxid"));
				ddxxBean2.setYhxxid(rs.getInt("yhxxid"));
				ddxxBean2.setJyzt(rs.getInt("jyzt"));
				ddxxBean2.setJyze(rs.getFloat("jyze"));
				ddxxBean2.setDdjlsj(rs.getString("ddjlsj"));
				ddxxBean2.setFhfs(rs.getInt("fhfs"));
				ddxxBean2.setShdz(rs.getString("shdz"));
				ddxxBean2.setSjhm(rs.getString("sjhm"));
				ddxxBean2.setYb(rs.getString("yb"));
				ddxxBean2.setShrxm(rs.getString("shrxm"));
				lstDdxx.add(ddxxBean2);
			}
			rs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
			return lstDdxx;
	}
	
	
	/**
	 * 获取总条数
	 */
	public int findDdxxCnt(DdxxBean ddxxBean){
		DataBaseAccess dba=null;
		ResultSet rs=null;
		int iCt=0;
		try {
			dba=new DataBaseAccess();
			rs=dba.executeQuery(ddxxQuery.getDdxxCnt(ddxxBean));
			if(rs.next()){
				iCt=rs.getInt("ct");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return iCt;
	}
	/***
	 * 查找单条
	 * @return
	 */
	public DdxxBean findDdxxBean(String ddxxid){
		DdxxBean ddxxBean=new DdxxBean();
		try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(ddxxQuery.findDdxxSql());
			ps.setString(1,ddxxid.trim() );
			rs=ps.executeQuery();
			while(rs.next()){
				ddxxBean.setDdxxid(rs.getString("ddxxid"));
				ddxxBean.setYhxxid(rs.getInt("yhxxid"));
				ddxxBean.setShxxid(rs.getInt("shxxid"));
				ddxxBean.setFkzt(rs.getInt("fkzt"));
				ddxxBean.setFhzt(rs.getInt("fhzt"));
				ddxxBean.setJyze(rs.getFloat("jyze"));
				ddxxBean.setDdjlsj(rs.getDate("ddjlsj").toString());
				ddxxBean.setJyzt(rs.getInt("jyzt"));
				ddxxBean.setDdjlsj(rs.getDate("ddjlsj").toString());
				ddxxBean.setFhfs(rs.getInt("fhfs"));
				ddxxBean.setShdz(rs.getString("shdz"));
				ddxxBean.setSjhm(rs.getString("sjhm"));
				ddxxBean.setYb(rs.getString("yb"));
				ddxxBean.setShrxm(rs.getString("shrxm"));
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ddxxBean;
	}
	/**
	 * 插入订单信息
	 * @param ddxx
	 * @return
	 */
    public Boolean insertDdxx(DdxxBean ddxx){
    	int ddxxinsert=0;
    	try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(ddxxQuery.getInsertDdxxQuery());
			ps.setString(1, ddxx.getDdxxid());
			ps.setInt(2, ddxx.getYhxxid());
			ps.setInt(3, ddxx.getShxxid());
			ps.setString(4, ddxx.getDdjlsj());
			ps.setInt(5, ddxx.getFhfs());
			ps.setInt(6, ddxx.getFkzt());
			ps.setInt(7, ddxx.getFhzt());
			ps.setInt(8, ddxx.getJyzt());
			ps.setFloat(9, ddxx.getJyze());
			ps.setString(10, ddxx.getShdz());
			ps.setString(11, ddxx.getSjhm());
			ps.setString(12, ddxx.getYb());
			ps.setString(13, ddxx.getShrxm());
			ddxxinsert=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				dba.releaseConnect();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (ddxxinsert==1) {
			return true;
		}else {
			return false;
		}
    }
    
    public boolean updateDdxxJyzt(DdxxBean bean) throws Exception{
    	int iFlag=0;
    	try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(ddxxQuery.updateDdxxJyzt());
			ps.setInt(1,bean.getJyzt());
			ps.setString(2, bean.getDdxxid());
			iFlag=ps.executeUpdate();
			dba.commit();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		if(iFlag>0){
			return true;
		}else{
			return false;
		}
    }
    /**
     * 获取订单信息
     * @param yhxxid
     * @return
     */
    public List<DdxxBean> findDdxxlst(int yhxxid) throws Exception{
    	List<DdxxBean> lstDdxx=new ArrayList<DdxxBean>();
        try {
			dba=new DataBaseAccess();
			ps=dba.getPstmt(ddxxQuery.findDdxx());
			ps.setInt(1, yhxxid);
			rs=ps.executeQuery();
			while(rs.next()){
				DdxxBean ddxxBean=new DdxxBean();
				ddxxBean.setDdxxid(rs.getString("ddxxid"));
				ddxxBean.setDdjlsj(rs.getString("ddjlsj"));
				lstDdxx.add(ddxxBean);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			dba.rollback();
			e.printStackTrace();
		}finally{
			dba.releaseConnect();
		}
		return lstDdxx;
     }
    
}
